<template>
  <SplitLayout :leftWidth="'250px'">
    <div slot="left" class="left">
      <AreaTree class="treeBox" @selectItem="onSelectItem" />
    </div>
    <div slot="top" class="header-top" v-if="!isDetail">
      <a-form-model v-bind="formItemLayout" class="formBox">
        <a-col :span="6">
          <a-form-model-item label="位置">
            <a-input v-model="pageFilter.position" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
        <a-col :span="6">
          <a-form-model-item label="日期">
            <a-range-picker
              v-model="rangePickerTime"
              :allowClear="true"
              valueFormat="YYYY-MM-DD"
              format="YYYY-MM-DD"
              @change="rangePickerChange"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="6">
          <a-form-model-item label="状态">
            <a-select placeholder="请选择" v-model="pageFilter.status">
              <a-select-option value="">全部</a-select-option>
              <a-select-option value="1">在线</a-select-option>
              <a-select-option value="2">离线</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="6">
          <a-form-model-item :wrapper-col="{ offset: 0 }">
            <a-space style="padding-left: 20px;">
              <a-button type="primary" @click="onQuery"> 查询 </a-button>
              <a-button @click="onResetForm"> 重置 </a-button>
            </a-space>
          </a-form-model-item>
        </a-col>
      </a-form-model>
    </div>
    <div class="con-content" v-if="!isDetail">
      <a-button style="margin-bottom:10px;" :load="confirmLoading" type="primary" @click="onImport"> 导入 </a-button>
      <div class="con-content-list">
        <z-table
          :showAlert="false"
          :columns="columns"
          :loading="loading"
          :dataSource="pageData"
          :showSelection="false"
          @change="onTableChange"
        >
          <template slot="order" slot-scope="text, record, index">
            {{ index + 1 }}
          </template>
          <template slot="status" slot-scope="text">
            <span v-if="text == 1">在线</span>
            <span style="color:red;" v-if="text == 2">离线</span>
          </template>
          <template slot="action" slot-scope="text, record">
            <a @click="onView(record)">查看</a>
          </template>
        </z-table>
      </div>
    </div>
    <div v-if="isDetail" class="detailBox">
      <div class="playerBox">
        <VideoPlayer ref="videoPlayerId" />
        <div class="carouselBox">
          <a-carousel arrows>
            <div slot="prevArrow" slot-scope="props" class="custom-slick-arrow" style="left: -18px; zindex: 1">
              <a-icon type="left" />
            </div>
            <div slot="nextArrow" slot-scope="props" class="custom-slick-arrow" style="right: 5px">
              <a-icon type="right" />
            </div>
            <div v-for="(item, key) in imgArr" :key="key">
              <div v-for="(item2, key) in item.imgs" :key="key" class="hotBox">
                <img :src="item2.src" style="cursor: pointer" @click="onGunClick(item2.videoUrl)" />
              </div>
            </div>
          </a-carousel>
        </div>
      </div>
      <div class="infoBox">
        <p class="p1">{{ detailInfo.deviceName }}</p>
        <p class="p2">位置：{{ detailInfo.position }}</p>
        <p class="p3">经纬度：{{ detailInfo.longitude }} , {{ detailInfo.latitude }}</p>
        <!-- <div>
          <span>日期</span>
          <a-date-picker style="width: 60%;margin-left:10px;" placeholder="请选择" v-model="detailInfo.date" />
        </div> -->
        <span style="font-size:16px;">回放日期：</span>
        <div>
          <a-range-picker
            v-model="rangePickerTime1"
            format="YYYY-MM-DD HH:mm:ss"
            valueFormat="YYYY-MM-DD HH:mm:ss"
            show-time
            allowClear
            @change="rangePickerChange1"
            style="width:100%;margin: 5px 0 10px 0;"
          />
        </div>
        <a-button type="primary" @click="streamReplay"> 播放录像 </a-button>
      </div>
    </div>
    <j-import-modal ref="importModal" :url="getImportUrl()" @ok="importOk" />
  </SplitLayout>
</template>
<script>
import SplitLayout from '@/components/zxd/layouts/SplitLayout'
import * as api from '@/api/xlgc/videoManage.js'
import AreaTree from '@/views/xlgc/modules/areaTree.vue'
import VideoPlayer from './components/videoPlayer.vue'
export default {
  components: {
    SplitLayout,
    AreaTree,
    VideoPlayer
  },
  data() {
    return {
      pageData: {},
      loading: false,
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          scopedSlots: { customRender: 'order' },
          width: 100,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` // 序号分页后连续
        },
        {
          title: '设备名称',
          dataIndex: 'deviceName',
          align: 'left'
          // width: 200
        },
        {
          title: '位置',
          dataIndex: 'position',
          align: 'center'
          // width: 150
        },
        {
          title: '日期',
          dataIndex: 'createTime',
          align: 'center',
          width: 230,
          scopedSlots: { customRender: 'createTime' }
        },
        {
          title: '状态',
          dataIndex: 'status',
          align: 'center',
          // width: 140,
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '操作',
          key: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
          width: 100
        }
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
        areaCode: '' // 区域资源唯一编码/ID
      },
      streamFilter: {
        cameraId: '' // 摄像头监控区域ID
      },
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 19 }
      },
      rangePickerTime: [],
      rangePickerTime1: [],
      confirmLoading: false,
      isDetail: false,
      detailInfo: {},
      imgArr: [
        {
          imgs: [
            {
              src: 'https://www.baidu.com/img/bd_logo1.png',
              videoUrl: 'http://vjs.zencdn.net/v/oceans.mp4'
            },
            {
              src: 'https://www.baidu.com/img/bd_logo1.png',
              videoUrl: 'https://cdn.theguardian.tv/webM/2015/07/20/150716YesMen_synd_768k_vp8.webm'
            },
            {
              src: 'https://www.baidu.com/img/bd_logo1.png',
              videoUrl: 'http://vjs.zencdn.net/v/oceans.mp4'
            },
            {
              src: 'https://www.baidu.com/img/bd_logo1.png',
              videoUrl: 'http://vjs.zencdn.net/v/oceans.mp4'
            },
            {
              src: 'https://www.baidu.com/img/bd_logo1.png',
              videoUrl: 'http://vjs.zencdn.net/v/oceans.mp4'
            }
          ]
        },
        {
          imgs: [
            {
              src: 'https://www.baidu.com/img/bd_logo1.png'
            },
            {
              src: 'https://www.baidu.com/img/bd_logo1.png'
            },
            {
              src: 'https://www.baidu.com/img/bd_logo1.png'
            },
            {
              src: 'https://www.baidu.com/img/bd_logo1.png'
            },
            {
              src: 'https://www.baidu.com/img/bd_logo1.png'
            }
          ]
        }
      ]
    }
  },

  created() {
    let id = this.$route.query.id
    if (id != undefined) {
      // 重点监控区域，点击设备跳转进来
      this.isDetail = true
      this.streamFilter.cameraId = id
      this.getDetail(id)
    } else {
      this.onQuery()
    }
  },
  mounted() {},
  methods: {
    onSelectItem(code) {
      this.isDetail = false
      this.pageFilter.areaCode = code
      this.onQuery()
    },
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    async refreshTable() {
      this.loading = true
      let res = await api.lhAreaList(this.pageFilter)
      if (res.success) {
        this.loading = false
        this.pageData = res.result
      }
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    onResetForm() {
      this.pageFilter = {
        pageNo: 1,
        pageSize: 10
      }
      this.rangePickerTime = []
      this.refreshTable()
    },
    onImport() {
      this.$refs.importModal.show()
    },
    getImportUrl() {
      // return dataApi.equipmentImportUrl
    },
    importOk() {
      this.onQuery()
    },
    onGunClick(url) {
      this.$refs.videoPlayerId.setVideoUrl(url)
    },
    // 购买时间
    rangePickerChange(date) {
      this.$set(this.pageFilter, 'beginDate', date[0])
      this.$set(this.pageFilter, 'endDate', date[1])
    },
    rangePickerChange1(date) {
      this.$set(this.streamFilter, 'beginDateTime', date[0])
      this.$set(this.streamFilter, 'endDateTime', date[1])
    },
    onView(record) {
      this.isDetail = true
      this.streamFilter.cameraId = record.id
      this.getDetail(record.id)
    },
    async getDetail(id) {
      let res = await api.deviceDetail({ id: id })
      if (res.success) {
        this.loading = false
        this.detailInfo = res.result
      }
    },
    // 获取监控设备视频流地址
    async streamReplay() {
      let res = await api.streamReplay(this.streamFilter)
      if (res.success) {
        // this.detailInfo = res.result
      }
    }
  }
}
</script>
<style scoped lang="less">
.treeBox {
  width: 95%;
  margin: 0px auto;
  border: 1px solid #ccc;
  padding: 10px;
}
.header-top {
  position: relative;
  // height: 600px;
  .formBox {
    position: relative;
    top: 11px;
    left: 20px;
  }
}
.con-content-top {
  margin-bottom: 20px;
}
/deep/.ant-layout-content {
  // padding-right: 10px !important;
}
.detailBox {
  overflow: hidden;
  .playerBox {
    float: left;
    width: 68%;
  }
  .infoBox {
    float: left;
    margin-left: 10px;
    padding: 10px 0 0 10px;
    width: 30%;
    height: 570px;
    .p1 {
      font-size: 18px;
      font-weight: bold;
    }
    background-color: #fafafa;
  }
}
.carouselBox {
  margin-top: 20px;
}
.carouselBox .ant-carousel .slick-slide {
  height: 300px;
  overflow: hidden;
  background: #fff;
}
.carouselBox .ant-carousel .slick-slide .hotBox {
  width: 16%;
  float: left;
  margin-left: 2.5%;
}
.carouselBox .ant-carousel .slick-slide .hotBox img {
  width: 100%;
  height: 100px;
  cursor: pointer;
}
.carouselBox .ant-carousel .custom-slick-arrow {
  width: 25px;
  height: 25px;
  font-size: 25px;
  color: #000;
  // background-color: rgba(31, 61, 35, 0.11);
  opacity: 0.5;
}
.carouselBox .ant-carousel .custom-slick-arrow:before {
  display: none;
}
.carouselBox .ant-carousel .custom-slick-arrow:hover {
  opacity: 0.7;
}
</style>
